/**
 * 漆水河 脚手架基础公开课
 * 2022/8/13 --- 2022/12/13
 * 有疑问可加QQ群: 809126489，后续会持续输出与大家一起成长
 */
package com.qishuihe.common.domain.role.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qishuihe.baseinterface.constant.CommonConstant;
import com.qishuihe.common.domain.role.entity.PermissionEntity;
import com.qishuihe.common.domain.role.mapper.PermissionMapper;
import com.qishuihe.common.domain.role.service.RolePermissionRelationService;
import com.qishuihe.common.domain.role.entity.RolePermissionRelationEntity;
import com.qishuihe.common.domain.role.mapper.RolePermissionRelationMapper;
import com.qishuihe.baseinterface.basevo.vo.ResultVO;
import com.qishuihe.outbound.user.RolePermissionDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RolePermissionRelationServiceImpl extends ServiceImpl<RolePermissionRelationMapper, RolePermissionRelationEntity> implements RolePermissionRelationService {

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public ResultVO<RolePermissionDTO> modfiyRolePermission(RolePermissionDTO rolePermissionDTO) {
        //todo 1.判断权限点ID是否存在，不存在新增
        //todo 2.将权限点ID与校色ID绑定入库
        ResultVO<RolePermissionDTO> resultDto =  new ResultVO<RolePermissionDTO>();
        RolePermissionRelationEntity rolePermissionRelationEntity = new RolePermissionRelationEntity();
        BeanUtils.copyProperties(rolePermissionDTO,rolePermissionRelationEntity);
        if(rolePermissionDTO.getPermissionId()==null){
            PermissionEntity permissionEntity = new PermissionEntity();
            permissionEntity.setUrl(rolePermissionDTO.getUrl());
            permissionEntity.setDescription(rolePermissionDTO.getDescription());
            permissionMapper.insert(permissionEntity);
            rolePermissionRelationEntity.setPermissionId(permissionEntity.getId());
        }
        if(!super.saveOrUpdate(rolePermissionRelationEntity)){
            resultDto.setCode(CommonConstant.STATUS_ERROR);
            resultDto.setMessage("修改用户失败");
            resultDto.setObj(rolePermissionDTO);
        }else {
            BeanUtils.copyProperties(rolePermissionRelationEntity, rolePermissionDTO);
            resultDto.setObj(rolePermissionDTO);
        }
        return resultDto;
    }

}